
const { resolve } = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
// 单独抽离出来css文件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

// 压缩css文件
const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin");

// 复用loader
const commonCssLoader = [
    // "style-loader",
    MiniCssExtractPlugin.loader,
    "css-loader",
    // 对css做兼容性处理,还需要再borwserslist中定义哪种模式下，哪些版本的浏览器需要做兼容性处理
    {
        loader: "postcss-loader",
        options: {
            ident: "postcss",
            plugins: () => [
                require("postcss-preset-env")
            ]
        }
    }
]

module.exports = {
    entry: "./src/index.js",
    output: {
        filename: "build.js",
        path: resolve(__dirname, "build")
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [...commonCssLoader]
            },
            {
                test: /\.less$/,
                use: [...commonCssLoader, "less-loader"]
            },
            /*
                正常来讲，一个文件只能被一个loader处理
                当一个文件要被多个loader处理的时候，那么一定要指定loader执行的先后顺序
                    先执行eslint 再执行babel  enforce:"pre"
            */
            {
                // js语法检查 使用airbnb规范，同时需要配置package.json中的eslintConfig：{ "extends": "airbnb-base" }
                test: /\.js$/,
                exclude: /node_module/,
                // 对于js文件，优先执行这里面的loader
                enforce: "pre",
                loader: "eslint-loader",
                options: {
                    fix: true
                }
            },
            {
                // 对js做兼容性处理
                test: /\.js$/,
                exclude: /node_module/,
                loader: "babel-loader",
                options: {
                    presets: [
                        [
                            "@babel/preset-env",
                            {
                                useBuiltIns: "usage",
                                corejs: { version: 3 },
                                target: {
                                    chrome: "60",
                                    firefox: "50"
                                }
                            }
                        ]
                    ]
                }
            },
            {
                test: /\.(jpg|png|gif)$/,
                loader: "url-loader",
                options: {
                    limit: 8 * 1024,
                    name: "[hash:10].[ext]",
                    outputPath: "imgs",
                    esModule: false
                }
            },
            {
                // html中的img
                test: /\.html$/,
                loader: "html-loader"
            },
            {
                // 其他资源
                exclude: /\.(js|html|gif|jpg|png|css|less)/,
                loader: "file-loader",
                options: {
                    outputPath: "media"
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: "./src/index.html",
            minify: {
                collapseWhitespace: true,
                removeComments: true
            }
        }),
        new MiniCssExtractPlugin({
            filename: "css/build.css"
        }),
        new OptimizeCssAssetsWebpackPlugin()
    ],
    mode: "production"
}


